/*
 * Copyright (C) 2011 Jake Wharton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tyersoft.kickcalendar;

import java.util.ArrayList;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.TypedValue;
import android.view.View;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.tyersoft.kickcalendar.adapters.DatePickerPreference;
import com.tyersoft.kickcalendar.calendar.CalendarUtility;
import com.tyersoft.slideout.SlideoutActivity;

public class Preference extends SherlockPreferenceActivity implements android.content.SharedPreferences.OnSharedPreferenceChangeListener
	// implements DatePickerDialog.OnDateSetListener 
{
	
    //private OnPreferenceChangeListener onSharedPreferenceChanged;

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar
    	/*
        boolean isLight = false; //SampleList.THEME == R.style.Theme_Sherlock_Light;

        menu.add("Save")
            .setIcon(isLight ? R.drawable.ic_compose_inverse : R.drawable.ic_compose)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        menu.add("Search")
            .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        menu.add("Refresh")
            .setIcon(isLight ? R.drawable.ic_refresh_inverse : R.drawable.ic_refresh)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		*/
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //setTheme(SampleList.THEME); //Used for theme switching in samples
    	
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        
        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
        sp.registerOnSharedPreferenceChangeListener(this);
        setSummaries(sp);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
		findViewById(R.id.abs__home).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
						try {
							SlideoutActivity.prepare(Preference.this, R.id.inner_content, width);
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
						startActivity(new Intent(Preference.this,
								MenuActivity.class));
						overridePendingTransition(0, 0);
					}
				});

        //This is a workaround for http://b.android.com/15340 from http://stackoverflow.com/a/5852198/132047
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
            bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);

            BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
            bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
        }
        
    }
    
	private void setSummaries(SharedPreferences preferences) {
		String sPreferenceKey, sPreferenceSummary;
		Integer iPreferenceValue;
		
		sPreferenceKey = getString(R.string.pref_person_name_key);
		sPreferenceSummary = preferences.getString(sPreferenceKey, "");
		if (sPreferenceSummary.length() == 0)
			sPreferenceSummary = getString(R.string.pref_person_name_summary);
		((android.preference.Preference) findPreference(sPreferenceKey)).setSummary(sPreferenceSummary);

		sPreferenceKey = getString(R.string.pref_person_birthday_key);
		sPreferenceSummary = preferences.getString(sPreferenceKey, "");
		if (sPreferenceSummary.length() == 0)
			sPreferenceSummary = getString(R.string.pref_person_birthday_summary);
		if (((android.preference.Preference) findPreference(sPreferenceKey)) != null)
		((android.preference.Preference) findPreference(sPreferenceKey)).setSummary(sPreferenceSummary);

		sPreferenceKey = getString(R.string.pref_person_sex_key);
		sPreferenceSummary = preferences.getString(sPreferenceKey, "");
		if (sPreferenceSummary.length() == 0)
			sPreferenceSummary = getString(R.string.pref_person_sex_summary);
		String[] listPersonSex = getResources().getStringArray(R.array.pref_person_sex_entries);
		if (sPreferenceSummary.equals("M")) sPreferenceSummary = listPersonSex[0];
		if (sPreferenceSummary.equals("F")) sPreferenceSummary = listPersonSex[1];
		((android.preference.Preference) findPreference(sPreferenceKey)).setSummary(sPreferenceSummary);

		sPreferenceKey = getString(R.string.pref_basic_calendar_key);
		sPreferenceSummary = CalendarUtility.getBasicCalendarName(this);
		((android.preference.Preference) findPreference(sPreferenceKey)).setSummary(sPreferenceSummary);
		((android.preference.ListPreference) findPreference(sPreferenceKey)).setEntries(CalendarUtility.CalendarEntries());
		((android.preference.ListPreference) findPreference(sPreferenceKey)).setEntryValues(CalendarUtility.CalendarEntryValues());
		
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		setSummaries(sharedPreferences);
	}
    
}
